/**
 * create by young (young2f@outlook.com) on 2020/12/4
 * 采购物料记录
 */
'use strict';
const model = require('../base/tModelBase');
const _ = require('lodash');
module.exports = app => {
  const mongoose = app.mongoose;
  const schema = mongoose.Schema;
  const connect = app.mongooseDB.get('pmcBus');
  const json = {
    isRent: { type: Boolean, _type: 'boolean', title: '是否租用', enableArea: [ 'table', 'search' ], },
    procureOrderCode: { type: String, _type: 'string', title: '采购单编码', enableArea: [ 'table', 'search' ], },
    materialType: {
      type: 'string', _type: 'select', table: 'division', title: '材料类型', required: true, enableArea: [
        'table', 'input',
        'detail',
        'search'
      ],
      options: [ '主营业务采购', '其它采购' ]
    },
    division: { type: String, _type: 'string', title: '事业部', enableArea: [ 'table', 'search' ], },
    pmLotId: { type: String, _type: 'string', title: '采购物料批次编码', required: true },
    code: { type: String, _type: 'string', title: '物料编码', required: true },
    name: { type: 'string', inputType: 'string', title: '物料名称', enableArea: [ 'table', 'input', 'detail', ], },
    subcategory: {
      type: 'string',
      inputType: 'string',
      title: '物料小类',
      enableArea: [ 'table', 'input', 'detail', ],
    },
    spec: { type: 'string', inputType: 'string', title: '规格', enableArea: [ 'table', 'input', 'detail', ], },
    type: { type: 'string', inputType: 'string', title: '类别', enableArea: [ 'table', 'input', 'detail', ], },
    number: { type: 'string', inputType: 'string', title: '编号', enableArea: [ 'table', 'input', 'detail', ], },
    content : { type: 'string', inputType: 'string', title: '含量', enableArea: [ 'table', 'input', 'detail', ], },
    vendor: { type: String, _type: 'string', title: '供应商名称', },
    vendorCode: { type: String, _type: 'string', title: '供应商编码' },
    taxRate: { type: String, _type: 'string', title: '税率' },
    target: { type: 'string', inputType: 'string', title: '去向', enableArea: [ 'table', 'input', 'detail', ], },
    customer: { type: 'string', inputType: 'string', title: '去向客户', enableArea: [ 'table', 'input', 'detail', ], },
    customerProject: { type: 'string', inputType: 'string', title: '客户项目', enableArea: [ 'table', 'input', 'detail', ], },
    customerCode: {
      type: 'string',
      inputType: 'string',
      title: '去向客户编码',
      enableArea: [ 'table', 'input', 'detail', ],
    },

    reviewStatus: {
      type: String,
      _type: 'string',
      title: '审核状态',
      enableArea: [ 'table', 'search' ],
      default: '待审核'
    },
    inboundStatus: {
      type: String,
      _type: 'string',
      title: '入库状态',
      enableArea: [ 'table', 'search' ],
      default: '待入库'
    },
    acceptanceStatus: {
      type: String,
      _type: 'string',
      title: '验收状态',
      enableArea: [ 'table', 'search' ,'detail'],
      default: '待验收'
    },

    price: { type: Number, _type: 'number', title: '单价', enableArea: [ 'table', 'search' ], },
    procureShippingFee: { type: Number, _type: 'number', title: '运费', enableArea: [ 'table', 'search' ], },
    count: { type: Number, _type: 'number', title: '采购数量', enableArea: [ 'table', 'search' ], },
    useCount: { type: Number, _type: 'number', title: '占用数量', enableArea: [ 'table', 'search' ], default: 0 },
    inboundCount: { type: Number, _type: 'number', title: '入库数量', enableArea: [ 'table', 'search' ], default: 0 },
    outboundCount: { type: Number, _type: 'number', title: '出库数量', enableArea: [ 'table', 'search' ], default: 0 },
    inboundDate: { type: Date, _type: 'date', title: '最近入库时间', enableArea: [ 'table', 'search' ] },
    salesOrders: { type: Array, _type: 'array', title: '占用订单', enableArea: [ 'table', 'search' ] },
    invoicingCount: { type: Number, _type: 'number', title: '开票数量',default:0},
  };
  const _model = _.cloneDeep(model);
  _.assign(_model, json);
  const _schema = new schema(
    _model,
    { timestamps: { createdAt: 'createdTs', updatedAt: 'updatedTs' } }
  );
  return connect.model('ProcureMaterialRecord', _schema);
};
